/* 
 * PROJECT: FLARManager
 * http://transmote.com/flar
 * Copyright 2009, Eric Socolofsky
 * --------------------------------------------------------------------------------
 * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project.
 *	http://www.libspark.org/wiki/saqoosha/FLARToolKit
 * FLARToolkit is Copyright (C)2008 Saqoosha,
 * and is ported from NYARToolkit, which is ported from ARToolkit.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * For further information please contact:
 *	<eric(at)transmote.com>
 *	http://transmote.com/flar
 * 
 */

package com.transmote.flar.utils {
	import com.transmote.flar.marker.FLARMarker;
	import com.transmote.flar.marker.FLARMarkerEvent;
	import com.transmote.flar.marker.FLARMarkerOutline;
	import com.transmote.flar.pattern.FLARPattern;
	
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
	import org.libspark.flartoolkit.core.types.FLARDoublePoint2d;
	
	/**
	 * FLARProxy provides a way to test FLARToolkit applications with a mouse and keyboard.
	 * to use, instantiate a FLARProxy instance and call FLARProxy.activate().
	 * set up handlers for FLARMarkerEvents in the same manner as with FLARManager.
	 * 
	 * clicking the mouse sends a MARKER_ADDED event,
	 * dragging the mouse sends a MARKER_UPDATED event,
	 * and releasing the mouse sends a MARKER_REMOVED event.
	 * these events can be handled exactly as if they were dispatched by FLARManager.
	 * 
	 * press keys 0-9 to specify a patternId.
	 * FLARProxy only supports patternIds 0 through 9.
	 */ 
	public class FLARProxy extends EventDispatcher {
		private static const MAX_NUMBER_MARKERS:uint = 10;
		private static const DEFAULT_MARKER_Z:Number = 400;
		
		private var downsampleRatio:Number;
		private var stage:Stage;
		private var markerCornerVal:Number;
		private var mouseIsDown:Boolean;
		private var activePatternId:uint = 0;
		private var activeMarkers:Vector.<FLARMarker>;
		
		
		/**
		 * constructor.
		 * @param	downsampleRatio		similar to IFLARSource, downsampleRatio is the ratio of the screen size
		 * 								as reported to FLARToolkit to the actual screen size.  defaults to 0.5,
		 * 								to match FLARCameraSource and FLARLoaderSource.
		 */
		public function FLARProxy (downsampleRatio:Number=0.5) {
			this.downsampleRatio = downsampleRatio;
			this.markerCornerVal = 0.5 * this.downsampleRatio * FLARPattern.DEFAULT_UNSCALED_MARKER_WIDTH;
			this.activeMarkers = new Vector.<FLARMarker>(MAX_NUMBER_MARKERS, true);
		}
		
		/**
		 * begin marker simulation.
		 * @param	stage	a reference to the application Stage.
		 */
		public function activate (stage:Stage) :void {
			this.stage = stage;
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
		}
		
		/**
		 * stop marker simulation.
		 */
		public function deactivate () :void {
			this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
			this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown);
		}
		
		private function addMarker (patternId:uint, x:Number, y:Number) :void {
			var proxyMarker:FLARMarker = new FLARMarker(patternId, 0, 100, this.createProxyOutline(x, y), this.createProxyMatrix(x, y));
			this.activeMarkers[patternId] = proxyMarker;
			this.dispatchEvent(new FLARMarkerEvent(FLARMarkerEvent.MARKER_ADDED, proxyMarker));
		}
		
		private function updateMarker (patternId:uint, x:Number, y:Number) :void {
			var proxyMarker:FLARMarker = this.activeMarkers[patternId];
			if (!proxyMarker) { return; }
			
			var updatedMarker:FLARMarker = new FLARMarker(patternId, 0, 100, this.createProxyOutline(x, y), this.createProxyMatrix(x, y));
			proxyMarker.copy(updatedMarker);
			this.dispatchEvent(new FLARMarkerEvent(FLARMarkerEvent.MARKER_UPDATED, proxyMarker));
		}
		
		private function removeMarker (patternId:uint, x:Number, y:Number) :void {
			var proxyMarker:FLARMarker = this.activeMarkers[patternId];
			if (!proxyMarker) { return; }
			
			var updatedMarker:FLARMarker = new FLARMarker(patternId, 0, 100, this.createProxyOutline(x, y), this.createProxyMatrix(x, y));
			proxyMarker.copy(updatedMarker);
			this.activeMarkers[patternId] = null;
			this.dispatchEvent(new FLARMarkerEvent(FLARMarkerEvent.MARKER_REMOVED, proxyMarker));
		}
		
		private function onMouseDown (evt:MouseEvent) :void {
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
			this.mouseIsDown = true;
			this.addMarker(this.activePatternId, evt.stageX, evt.stageY);
		}
		
		private function onMouseMove (evt:MouseEvent) :void {
			this.updateMarker(this.activePatternId, evt.stageX, evt.stageY);
		}
		
		private function onMouseUp (evt:MouseEvent) :void {
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
			this.mouseIsDown = false;
			this.removeMarker(this.activePatternId, evt.stageX, evt.stageY);
		}
		
		private function onKeyDown (evt:KeyboardEvent) :void {
			var newPatternId:uint = evt.keyCode - 48;
			if (newPatternId == this.activePatternId || newPatternId < 0 || newPatternId > 9) { return; }
			
			trace("FLARProxy active patternId: "+ newPatternId);
			var lastPatternId:uint = this.activePatternId;
			this.activePatternId = newPatternId;
			
			if (this.mouseIsDown) {
				this.removeMarker(lastPatternId, this.stage.mouseX, this.stage.mouseY);
				this.addMarker(this.activePatternId, this.stage.mouseX, this.stage.mouseY);
			}
		}
		
		private function createProxyOutline (x:Number, y:Number) :FLARMarkerOutline {
			x *= this.downsampleRatio;
			y *= this.downsampleRatio;
			
			var sqvertex:Array = new Array(
				new FLARDoublePoint2d(x-this.markerCornerVal, y-this.markerCornerVal),
				new FLARDoublePoint2d(x+this.markerCornerVal, y-this.markerCornerVal),
				new FLARDoublePoint2d(x+this.markerCornerVal, y+this.markerCornerVal),
				new FLARDoublePoint2d(x-this.markerCornerVal, y+this.markerCornerVal));
			return new FLARMarkerOutline(null, sqvertex, null, null);
		}
		
		private function createProxyMatrix (x:Number, y:Number) :FLARTransMatResult {
			var matrix:FLARTransMatResult = FLARGeomUtils.createFLARIdentityTransMat();
			matrix.m03 = (x - 0.5*this.stage.stageWidth) * this.downsampleRatio;
			matrix.m13 = (y - 0.5*this.stage.stageHeight) * this.downsampleRatio;
			matrix.m23 = DEFAULT_MARKER_Z;
			return matrix;
		}
	}
}